package zhangtao.bwie.com.yuekao2;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Bean.CarBean;
import Presenter.IPresenter;
import Views.IView;
import butterknife.ButterKnife;

/**
 * Created by ZhangTAO on 2017/12/21.
 */

public class Cars_act extends AppCompatActivity implements View.OnClickListener, IView {
    private TextView bianji;
    private CheckBox quan_che;
    private TextView price_zongs;
    private TextView shop_zongs;
    private RecyclerView recy_father;
    private Button dele_btn;
    private IPresenter iPresenter;
    private List<CarBean.DataBean> shop_data;
    private MyAdapter_Father myAdapter_father;
    private HashMap<String, String> map;
    private int counts;
    private double moneys;
    private boolean zt = true;
    private Button price_btn;
    private Button shou_btn;
    private AlertDialog alertDialog;
    private Button fen_btn;
    private Handler ler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cars_act);
        ButterKnife.bind(this);
        Fresco.initialize(this);
        initView();
        setOnClick();
        map = new HashMap<>();
        map.put("uid", "97");
        setPresenter("http://120.27.23.105/", map);
    }

    private void bian_one() {
        for (int i = 0; i < shop_data.size(); i++) {
            shop_data.get(i).setIscheck(false);
            for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                shop_data.get(i).getList().get(j).setIscheck(false);
            }
        }
        quan_che.setChecked(false);
        bianji.setText("完成");
        //隐藏
        shop_zongs.setVisibility(View.INVISIBLE);
        price_zongs.setVisibility(View.INVISIBLE);
        price_btn.setVisibility(View.INVISIBLE);
        //显示
        fen_btn.setVisibility(View.VISIBLE);
        shou_btn.setVisibility(View.VISIBLE);
        dele_btn.setVisibility(View.VISIBLE);
        UpdateRecyclerView();
    }

    private void bian_two() {
        bianji.setText("编辑");
        quan_che.setChecked(true);
        price_zongs.setText("总价0.00");
        shop_zongs.setText("共0件商品");
        for (int i = 0; i < shop_data.size(); i++) {
            shop_data.get(i).setIscheck(true);
            for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                shop_data.get(i).getList().get(j).setIscheck(true);
            }
        }
        //显示
        shop_zongs.setVisibility(View.VISIBLE);
        price_zongs.setVisibility(View.VISIBLE);
        price_btn.setVisibility(View.VISIBLE);
        //隐藏
        fen_btn.setVisibility(View.INVISIBLE);
        shou_btn.setVisibility(View.INVISIBLE);
        dele_btn.setVisibility(View.INVISIBLE);
        UpdateRecyclerView();
    }

    private void setOnClick() {
        bianji.setOnClickListener(this);
        dele_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bianji:
                if (zt) {
                    bian_one();
                    dele_btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(final View view) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Cars_act.this);
                            builder.setTitle("提示");
                            builder.setMessage("是否确认删除" + counts + "件商品");
                            builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    List<CarBean.DataBean> father = new ArrayList<>();
                                    List<CarBean.DataBean.ListBean> chiled = new ArrayList<>();
                                    for (int i1 = 0; i1 < shop_data.size(); i1++) {
                                        for (int j = 0; j < shop_data.get(i1).getList().size(); j++) {
                                            if (shop_data.get(i1).getList().get(j).ischeck() == true) {
                                                chiled.add(shop_data.get(i1).getList().get(j));
                                            }
                                            if (shop_data.get(i1).ischeck() == true) {
                                                father.add(shop_data.get(i1));
                                            }
                                        }
                                    }
                                    for (int z = 0; z < shop_data.size(); z++) {
                                        shop_data.removeAll(father);
                                        for (int j = 0; j < shop_data.size(); j++) {
                                            shop_data.get(z).getList().removeAll(chiled);
                                        }
                                    }
                                    if (shop_data.size() == 0 || shop_data == null) {
                                        quan_che.setChecked(false);
                                    }
                                    UpdateRecyclerView();
                                    dialogInterface.dismiss();
                                }
                            });
                            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                            alertDialog = builder.create();
                            alertDialog.show();
                        }
                    });
                    zt = false;
                } else {
                    bian_two();
                    zt = true;
                    UpdateRecyclerView();
                }
                break;
        }
    }

    private void initView() {
        bianji = (TextView) findViewById(R.id.bianji);
        recy_father = (RecyclerView) findViewById(R.id.recy_father);
        quan_che = (CheckBox) findViewById(R.id.quan_che);
        price_zongs = (TextView) findViewById(R.id.price_zong);
        shop_zongs = (TextView) findViewById(R.id.shop_zong);
        dele_btn = (Button) findViewById(R.id.delete_btn);
        price_btn = (Button) findViewById(R.id.price_btn);
        shou_btn = (Button) findViewById(R.id.shou_btn);
        fen_btn = (Button) findViewById(R.id.fen_btn);
    }

    public void setPresenter(String url, Map<String, String> map) {
        iPresenter = new IPresenter();
        iPresenter.attach(this);
        iPresenter.getData(url, map);
    }

    public void setMyadapter() {
        myAdapter_father = new MyAdapter_Father(this, shop_data);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recy_father.setLayoutManager(manager);
        recy_father.setAdapter(myAdapter_father);
        setAdapter_zt();
    }

    private void setAdapter_zt() {
        quan_che.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                for (int i = 0; i < shop_data.size(); i++) {
                    if (b) {
                        shop_data.get(i).setIscheck(true);
                    } else {
                        shop_data.get(i).setIscheck(false);
                    }

                    for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                        if (b) {
                            shop_data.get(i).getList().get(j).setIscheck(true);
                        } else {
                            shop_data.get(i).getList().get(j).setIscheck(false);
                        }
                    }
                }
                counts = 0;
                moneys = 0;
                if (b) {
                    for (int i = 0; i < shop_data.size(); i++) {
                        for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                            if (shop_data.get(i).getList().get(j).ischeck()) {
                                counts += shop_data.get(i).getList().get(j).getNum();
                                moneys += shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < shop_data.size(); i++) {
                        for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                            if (shop_data.get(i).getList().get(j).ischeck()) {
                                counts += shop_data.get(i).getList().get(j).getNum();
                                moneys += shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();
                            }
                        }
                    }
                }
                shop_zongs.setText("共" + counts + "件商品");
                price_zongs.setText("总价:" + moneys);
                //更新
                UpdateRecyclerView();
            }
        });
        myAdapter_father.setCallBack(new MyAdapter_Father.allCheck() {
            @Override
            public void OnCheckLinstener_Father(boolean flag, int position) {
                shop_data.get(position).setIscheck(flag);
                for (int i = 0; i < shop_data.get(position).getList().size(); i++) {
                    shop_data.get(position).getList().get(i).setIscheck(flag);
                }
                if (allFatherSelect() == shop_data.size()) {
                    quan_che.setChecked(true);
                } else {
                    quan_che.setChecked(false);
                }
                counts = 0;
                moneys = 0;
                if (flag) {
                    for (int i = 0; i < shop_data.size(); i++) {
                        for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                            if (shop_data.get(i).getList().get(j).ischeck()) {
                                counts += shop_data.get(i).getList().get(j).getNum();
                                moneys += shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < shop_data.size(); i++) {
                        for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                            if (shop_data.get(i).getList().get(j).ischeck()) {
                                counts += shop_data.get(i).getList().get(j).getNum();
                                moneys += shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();
                            }
                        }
                    }
                }
                shop_zongs.setText("共" + counts + "件商品");
                price_zongs.setText("总价:" + moneys);
                //更新
                UpdateRecyclerView();
            }

            @Override
            public void OnItemCheckLinstener(boolean isSelected, int fatherposition, int chiledsposition) {
                //保存商品点击状态
                shop_data.get(fatherposition).getList().get(chiledsposition).setIscheck(isSelected);
                //通知店铺选择状态
                if (allChildSelect(fatherposition) == shop_data.get(fatherposition).getList().size()) {
                    shop_data.get(fatherposition).setIscheck(true);
                } else {
                    shop_data.get(fatherposition).setIscheck(false);
                }
                if (allFatherSelect() == shop_data.size()) {
                    quan_che.setChecked(true);
                } else {
                    quan_che.setChecked(false);
                }
                counts = 0;
                moneys = 0;
                for (int i = 0; i < shop_data.size(); i++) {
                    for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                        if (shop_data.get(i).getList().get(j).ischeck()) {
                            counts += shop_data.get(i).getList().get(j).getNum();
                            moneys += shop_data.get(i).getList().get(j).getNum() * shop_data.get(i).getList().get(j).getPrice();
                        }
                    }
                }
                shop_zongs.setText("共" + counts + "件商品");
                price_zongs.setText("总价:" + moneys);
                //更新
                UpdateRecyclerView();
            }

            @Override
            public void Onadd(int addnNum) {
                for (int i = 0; i < shop_data.size(); i++) {
                    for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                        if (shop_data.get(i).getList().get(j).ischeck()) {
                            counts = shop_data.get(i).getList().get(j).getNum();
                            ;
                            moneys = (addnNum * shop_data.get(i).getList().get(j).getPrice());
                        }
                    }
                }
                shop_zongs.setText("共" + counts + "件商品");
                price_zongs.setText("总价:" + moneys);
                //更新
                UpdateRecyclerView();
            }

            @Override
            public void Onjian(int jiannNum) {
                for (int i = 0; i < shop_data.size(); i++) {
                    for (int j = 0; j < shop_data.get(i).getList().size(); j++) {
                        if (shop_data.get(i).getList().get(j).ischeck()) {
                            counts = shop_data.get(i).getList().get(j).getNum();
                            ;
                            moneys = jiannNum * shop_data.get(i).getList().get(j).getPrice();
                        }
                    }
                }
                shop_zongs.setText("共" + counts + "件商品");
                price_zongs.setText("总价:" + moneys);
                //更新
                UpdateRecyclerView();
            }
        });

    }

    @Override
    public void onSuccess(Object o) {
        if (o instanceof CarBean) {
            CarBean bean = (CarBean) o;
            if (bean != null) {
                shop_data = bean.getData();
                setMyadapter();
            }
        }
    }

    @Override
    public void onFailed(Exception e) {

    }

    private void UpdateRecyclerView() {
        ler.post(new Runnable() {
            @Override
            public void run() {
                myAdapter_father.notifyDataSetChanged();
            }
        });
    }

    /**
     * 计算所有的商铺数量
     */
    private int allFatherSelect() {
        int sum = 0;
        for (int i = 0; i < shop_data.size(); i++) {
            if (shop_data.get(i).ischeck()) {
                sum++;
            }
        }
        return sum;
    }

    /**
     * 计算所有的商铺商品数量
     */
    private int allChildSelect(int position) {
        int sum = 0;
        for (int i = 0; i < shop_data.get(position).getList().size(); i++) {
            if (shop_data.get(position).getList().get(i).ischeck()) {
                sum++;
            }
        }
        return sum;
    }
}
